<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-07-07 15:58:41
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\Loom;

class LoomRepository extends Repositories
    {

    protected $relation_models = ['status', 'creator', 'updator', 'category', 'employee'];

    public function boot(Loom $model)
        {
        $this->model = $model;
        }
    public function fuzzySearch($value)
        {
        $value = trim($value);
        $this->where(function ($query) use ($value)
            {
            $query->orWhere("name", "like", "%{$value}%");
            $query->orWhere("code", "like", "%{$value}%");
            });
        }

    public function getParams()
        {
        $yarn_quantity = $material_id = 0;
        $request = request();
        if ($request->has("filter")) {
            $filters = $request->filter;
            foreach ($filters ?? [] as $keys => $filter) {
                foreach ($filter ?? [] as $key => $value) {
                    if ($key == 'yarn_quantity') {
                        $yarn_quantity = $filters[$keys]['yarn_quantity'];
                        unset($filters[$keys]['yarn_quantity']);
                        }
                    if ($key == 'material_id') {
                        $material_id = $filters[$keys]['material_id'];
                        unset($filters[$keys]['material_id']);
                        }
                    }
                }
            $request->offsetSet("filter", $filters);
            }
        return [$yarn_quantity, $material_id];
        }
    public function fetchRatio()
        {
        [$yarn_quantity, $material_id] = $this->getParams();
        $list                          = $this->condition()->getList();
        $category_id                   = MaterialRepository::whereId($material_id)->value('category_id');

        $category_ids = array_unique(MaterialCategoryRepository::category($category_id));

        return $list->map(function ($loom) use ($yarn_quantity, $category_ids)
            {
            //缸类型 变容量
            $loom->dosage = $loom->dosagetype;
            //默认分割水位比
            if ($yarn_quantity > 0) {
                $YarnWeight = $yarn_quantity / $loom->dosage * 1000;
                if ($loom->YarnWeight > $YarnWeight) {
                    $loom->ratio = $loom->YarnWeightRatio;
                    }
                }
            //水位比明细
            $data = $loom->details()->whereIn("category_id", $category_ids)->first();
            if ($data) {
                $data->dosage > 0 && $loom->dosage = $data->dosage;
                $data->ratio > 0 && $loom->ratio = $data->ratio;
                $data->rate > 0 && $loom->rate = $data->rate;
                $data->capacity > 0 && $loom->rate = $data->capacity;
                //装纱对应此缸的投纱。1000投纱，1400染缸，相当于此缸投纱比1000/1400=714
                if ($yarn_quantity > 0) {
                    $YarnWeight = $yarn_quantity / $loom->dosage * 1000;
                    //小于分隔浴比，用分隔浴比
                    if ($data->YarnWeight > $YarnWeight) {
                        $loom->ratio = $data->YarnWeightRatio;
                        }
                    }
                }
            //分割浴比明细
            if ($yarn_quantity > 0) {
                $data = $loom->ratios()
                    ->whereIn("category_id", $category_ids)
                    ->where('YarnWeight', ">=", $YarnWeight)
                    ->orderBy("YarnWeight")
                    ->first();
                if ($data) {
                    $loom->ratio = $data->ratio;
                    }
                }
            $loom->point_ratio = $loom->ratio = round($loom->ratio, 2);
            return $loom;
            });
        }
    }